<?php
	$project = new project();
	$admin = new admin();
	$action_ary = array(
		'add' => 'project_act_add',//项目的添加
		'address_add' => 'project_act_address_add',//项目作业点地址添加
		'bind_device' => 'project_act_bind_device',//项目绑定设备
		'bind_remove' => 'project_act_bind_remove',//解除项目绑定的设备
		'address_list' => 'project_act_address_list',//作业点的所在项目列表
		'nouse'=>'project_act_nouse', //设置记录无效
		'reback'=>'project_act_reback', //还原记录
		'picout'=>'project_act_picout',  //导出图片txt文档
		'delpic'=>'user_act_delpic',   //删除图片
		'delop'=>'user_act_delop',   //删除图片
        'delgas'=>'user_act_delgas', //删除重复重复数据
		'config'=>'project_act_config'
	);
	//执行函数
	if($action && ($fun = $action_ary[$action])){
		$fun();
	}


	//配置设置
	function project_act_config(){
		global $project;

		$rtn = array('state'=>false);
		$id = request('pid',false,true);
        if(empty($id)){
			$rtn['msg']='修改失败';
			exit(json_encode($rtn));
        }

		$a=request('a');
		$ary['a']=$a?trim($a):'';
		$b=request('b');
		$ary['b']=$b?trim($b):'';
		$c=request('c');
		$ary['c']=$c?trim($c):'';
		$x=request('x');
		$ary['x']=$x?trim($x):'';
		$y=request('y');
		$ary['y']=$y?trim($y):'';
		$z=request('z');
		$ary['z']=$z?trim($z):'';

		$data['config']=addslashes(var_export($ary, TRUE));
		$where="id=".$id;
		$project->set_db('syt_project',$data,false,$where);

		$rtn['state']=true;
		$rtn['msg']='失败成功';
		exit(json_encode($rtn));
	}


	function user_act_delop(){
		global $admin,$project,$_site;

		$rtn = array('state'=>false);
		$id = request('id',false,true);
		$cols='xid,img';
		$info=$project->recordinfo($id,$cols);

		if(empty($info)){
			$rtn['msg']='记录不存在';
			exit(json_encode($rtn));
		}

		$user_info = $admin->out_cookie();

		//非总管理
		if($user_info['group']!=6 && $user_info['id']!=$info['xid']){
			$rtn['msg']='非法操作数据！';
			exit(json_encode($rtn));
		}

		//unlink($_site->_root().$info['img']);
		$sql="DELETE FROM syt_checkin WHERE id={$id}";
		$admin->_mysql(0)->query($sql);

		$rtn['state']=true;
		exit(json_encode($rtn));
	}


    function user_act_delgas(){
        global $admin;
        $gas=new gas();

        $rtn = array('state'=>false);
        $id = request('id',false,true);
        $cols='id,xid';
        $info=$gas->getGas($id,$cols);

        if(empty($info)){
            $rtn['msg']='记录不存在';
            exit(json_encode($rtn));
        }

        $user_info = $admin->out_cookie();

        //非总管理
        if($user_info['group']!=6 && $user_info['id']!=$info['xid']){
            $rtn['msg']='非法操作数据！';
            exit(json_encode($rtn));
        }

        //unlink($_site->_root().$info['img']);
        $sql="DELETE FROM syt_gas WHERE id={$id}";
        $admin->_mysql(0)->query($sql);

        $rtn['state']=true;
        exit(json_encode($rtn));
    }



	//批量删除图片
	function user_act_delpic(){
		global $admin,$project,$_site;

		$rtn = array('state'=>false);
		//必须是总管理员
		$user_info = $admin->out_cookie();
		if($user_info['group']!=6){
			$rtn['msg']='必须是总管理员';
			exit(json_encode($rtn));
		}

		$uid=request('uid',false,true);  //项目经理
		$pid=request('pid',false,true);  //项目编号
		$date=request('date');  //图片日期
		$page=request('page',false,true,false,1); //图片页面
		$pagesize=50; // 每次删除图片的数量


		$data=compact('uid','pid','date');
		$list=$project->get_records($data,$page,$pagesize);
		if(empty($list['rows'])){
			$rtn['msg']='记录不存在了';
			exit(json_encode($rtn));
		}

		//批量删除图片
		foreach($list['rows'] as $val){
			unlink($_site->_root().$val['img']);
		}

		//最后一页
		$rtn['state']=true;
		if($list['cur_page']==$list['page_count']){
			//更新记录为已经全部删除
			$start_time=strtotime($date);
			$end_time=$start_time+86400;
			//$sql="UPDATE syt_checkin SET delimg=1 WHERE xid={$uid} AND pid={$pid} AND ctime>{$start_time} AND ctime<{$end_time}";
			$sql="DELETE FROM syt_checkin WHERE xid={$uid} AND pid={$pid} AND ctime>{$start_time} AND ctime<{$end_time}";
			$admin->_mysql(0)->query($sql);

			$rtn['finish']=true;
			exit(json_encode($rtn));
		}

		$rtn['finish']=false;
	    $rtn['msg']="共 {$list['row_count']} 条记录 ，已删除 ".$pagesize*$page;
		$rtn['page']=$page+1;
		exit(json_encode($rtn));
	}


	function project_act_picout(){
		global $project,$_site;

		$type=request('type',false,true,false,1);
		$uid=request('uid',false,true);
		$pid=request('pid',false,true);
		$date=request('date');

		//根据会员id查询记录，总管理查看全部记录
		$admin_obj = new admin();
		$info = $admin_obj->out_cookie();
		if(empty($info)){return false;}
		if(6!=$info['group'] && $info['id']!=$uid){
			exit('Right Error !!');
		}

		$list=$project->get_record($uid,$pid,$type,$date);
		if(empty($list)){
			exit('No Data !!');
		}

		$i=1;

		//加载站点配置文件
		$web_root=$_site->site_config['site']['img_url'];
		$url=$web_root?$web_root:'http://www.sytblb.com';
		$txt = count($list);
		foreach($list as $val){
			$txt .= "\r\n".$i." ".$url.$val['img'];
			$i++;
		}


		$type_ary=array(1=>'zc',2=>'js',3=>'hc');
		$filename=$date.'_'.$type_ary[$type].'_'.$pid.'.txt';
		Header( "Content-type:   application/octet-stream ");
		Header( "Accept-Ranges:   bytes ");
		header( "Content-Disposition:   attachment;   filename={$filename}");
		header( "Expires:   0 ");
		header( "Cache-Control:   must-revalidate,   post-check=0,   pre-check=0 ");
		header( "Pragma:   public ");
		echo $txt;
		exit();
	}




	/**
	 * 设置记录无效
	 * @author leiqianyong 2015-10-12 14:49 PM
	 */
	function project_act_nouse(){
		global $project;

		$id=request('id',false,true);
		$status=request('status',false,true,false,0); //是否修改后面的数据 0-不修改 1-取土 2-倒土
		$rtn = array('state'=>false);
		if(!$id){
			$rtn['msg'] = 1002;
			echo_json($rtn);
			exit;
		}

		$res = $project->project_record_state_change($id, 0);
		if($res){
			//修改后面记录的登记状态
			if($status>0){
				$cinfo = $project->recordinfo($id,'posi,dname,ctime');
				$posi = $status;
				$dname = $cinfo['dname'];
				$ctime = $cinfo['ctime'];
				$ctime_end = strtotime(date("Y-m-d",$ctime));

				//获取当天该车后面的记录
				$sql="select id from syt_checkin where type=3 and state=1 and dname='{$dname}' and ctime<{$ctime} and ctime>={$ctime_end} order by ctime desc";
				$clist=$project->_mysql(0)->find($sql);
				if($clist){
					$sql="UPDATE syt_checkin SET posi = CASE id ";
					foreach($clist as $vo){
						$sql .= "WHEN {$vo['id']} THEN ".$posi." ";
						$posi = 1==$posi?2:1;
						$ids[] = $vo['id'];
					}
					$idstr = implode(',',$ids);
					$sql .= "END WHERE id IN ({$idstr});";
					$project->_mysql(0)->query($sql,true);
					//error_log($sql."<br/>",3,'a.txt');
				}
			}


			$rtn['state'] = true;
			$rtn['msg'] = 1004;
			echo_json($rtn);
			exit;
		}




			$rtn['msg'] = 1003;
			echo_json($rtn);
	}


	/**
	 * 还原记录
	 * @author leiqianyong 2015-10-13 11:19 AM
	 */
	function project_act_reback(){
		global $project;

		$id=request('id',false,true);
		$rtn = array('state'=>false);
		if(!$id){
			$rtn['msg'] = 1002;
			echo_json($rtn);
			exit;
		}

		$res = $project->project_record_state_change($id, 1);
		if($res){
			$rtn['state'] = true;
			$rtn['msg'] = 1004;
			echo_json($rtn);
			exit;
		}

		$rtn['msg'] = 1003;
		echo_json($rtn);
	}



	/**
	* 获取作业点列表 ajax
	*
	*/
	function project_act_address_list(){
		global $project,$admin;
		$pid = request('pid',false,true);
		if(!$pid){
			echo_json(array('state'=>false,'msgs'=>'为获取到作业点'));
		}
		$res = $project->get_project_address_list(array('all'=>true,'pid'=>$pid));
		if($res){
			echo_json(array('state'=>false,'list'=>$res));
		}
		echo_json(array('state'=>false,'msgs'=>'为获取到作业点'));
	}
	/**
	* 项目作业点地址添加
	*
	*/
	function project_act_address_add(){
		global $project,$admin;
		if(!$admin->_id()){
			echo_json(array('state'=>false,'msg'=>1415));
		}
		$data['pid']=request('pid',false,true);
		$data['id']=request('id',false,true);
		if(!$data['pid']){
			echo_json(array('state'=>false,'msg'=>1441));
		}
		$data['title']=request('title');//作业地点名称
		if(!$data['title']){
			echo_json(array('state'=>false,'msg'=>1442));
		}
		$coord =request('coord');
		list($data['coord_x'],$data['coord_y']) = explode(',',$coord);
		if(!$data['coord_x'] && !$data['coord_y']){
			echo_json(array('state'=>false,'msg'=>1443));
		}
		$data['distance'] = request('distance',false,true);
		if($data['distance']<0){
			echo_json(array('state'=>false,'msg'=>1444));
		}
		if($data['id']){
			$s  = $project->address_data_ins_up($data);
			if(!$s){
				echo_json(array('state'=>false,'msg'=>1019));
			}
			echo_json(array('state'=>true,'msg'=>1018));
		}
		$s  = $project->address_data_ins_up($data);
		if(!$s){
			echo_json(array('state'=>false,'msg'=>1016));
		}
		echo_json(array('state'=>true,'msg'=>1015));
	}
	/**
	* 添加修改项目
	* @author xiezhihui 2014-6-9 16:40:40
	*/
	function project_act_add(){
		global $project,$admin;
		if(!$admin->_id()){
			echo_json(array('state'=>false,'msg'=>1415));
		}
		$data['id'] = request('id',false,true);//项目ID
		$data['uid'] = request('uid',false,true);//项目管理员ID
		if(!$data['uid']){
			echo_json(array('state'=>false,'msg'=>1432));
		}
		$data['title'] = request('title');//项目名称
		if(!$data['title']){
			echo_json(array('state'=>false,'msg'=>1433));
		}
		$data['start_time'] = request('start_time');//项目开始时间
		if(!$data['start_time']){
			echo_json(array('state'=>false,'msg'=>1434));
		}
		$data['start_time'] = strtotime($data['start_time']);
		$data['end_time'] = request('end_time');//项目结束时间
		if(!$data['end_time']){
			echo_json(array('state'=>false,'msg'=>1434));
		}
		$data['end_time'] = strtotime($data['end_time']);
		$data['state'] = request('state',false,true,false,1);//项目状态 1-开通 2-关闭
		$data['address'] = request('address');//项目施工地址
		if(!$data['address']){
			echo_json(array('state'=>false,'msg'=>1434));
		}
		$table = $project->get_table_name('table_project');

		//间隔时间
		$data['time']=request('time',false,true);
		//装车间隔时间
		$data['time_zc']=request('time_zc',false,true);

		if($data['id']){
			$s = $project->set_db($table,$data,false,"id={$data['id']}");
			if(!$s){
				echo_json(array('state'=>false,'msg'=>'1019'));
			}
			if($data['state'] != 2){
				echo_json(array('state'=>true,'msg'=>'1018'));
			}
			$rst = $project->project_remove_all_bind($data['id']);//项目关闭（完结）时解除其绑定的设备
			if(!$rst){
				echo_json(array('state'=>false,'msg'=>1446));
			}
			echo_json(array('state'=>true,'msg'=>1445));
		}
		$s = $project->set_db($table,$data);
		if($s){
			echo_json(array('state'=>true,'msg'=>'1015'));
		}
		echo_json(array('state'=>false,'msg'=>'1016'));
	}

	/**
	* 绑定设备
	* @author xiaxin 2014-06-10
	*
	*/
	function project_act_bind_device(){
		global $project,$admin;
		$msg = array('state'=>false,'msg'=>1415);
		if(!$admin->_id()){
			echo_json($msg);
		}

		//修改时的绑定记录id
		$bid=request('bid',false,true,false,0);
		$dev_id = request('device_id',false,true);
		if(!$bid){
			$is_bind = $project->check_device_bind($dev_id);
			if(!$dev_id || !$is_bind){
				$msg['msg'] = 1438;
				echo_json($msg);
			}
		}

		$project_id = request('project_id',false,true);
		$car_number = request('car_number');
		$doper = request('doper');
		$doper=$doper?$doper:'';

		$link_man = request('link_man');
		$phone = request('phone',false,true);
		$phone=$phone?$phone:'';
		$cartype=request('cartype');

		//油料管理员编号
		$gaser=trim(request('gaser'));
		//管理员姓名
		$gasername=trim(request('gasername'));

		//类型  1-工程车管理   2-油料管理
		$type=request('type',false,true,false,1);

		if(1==$type){
			if(!$project_id || !$link_man || !$car_number || !$cartype){
				$msg['msg'] = 1002;
				echo_json($msg);
			}

			$data['linkman']=$link_man;
			$data['car_number']=$car_number;
			$data['cartype']=$cartype;

		}elseif(2==$type){

			if(!$project_id){
				$msg['msg'] = 1002;
				echo_json($msg);
			}

			$data['gaser']=$gaser;
			$data['gasername']=$gasername;
		}

		$data['type']=$type;
		$data['pid']=$project_id;
		$data['did']=$dev_id;
		$data['time']=NOW;
		$data['state']=1;
		$data['phone']=$phone;
		$data['doper']=$doper;


		$rst = $project->project_device_bind_add($data,$bid);
		if(!$rst){
			$msg['msg'] = 1440;
			echo_json($msg);
		}
		$url="/admin/admin_project_device_list?id={$project_id}&type={$type}";
		$msg = array('state'=>true,'msg'=>1439,'url'=>$url);
		echo_json($msg);
	}

	/**
	* 解除项目绑定的设备
	* @author xiaxin 2014-06-11
	*
	*/
	function project_act_bind_remove(){
		global $project,$admin;
		$admin_info = $admin->out_cookie();
		/**
		if($admin_info['group'] != 6){//这点判读得有点死，必须是具有总管理员权限才能解绑
			location_msg(L(1415));
		}
		**/
		if(!$admin_info){
			location_msg(L(1415));
		}

		$id = request('id',false,true);
		$did = request('did',false,true);
		if(!$id || !$did){
			location_msg(L(1002));
		}

		//非总管理检查绑定归属问题
		if($admin_info['group'] != 6){
		//检查绑定信息
		$bind=$project->bind_info($id,'pid');
		if(!$bind){
			location_msg(L(1002));
		}
		//检查项目是否属于当前项目经理
		$bindower=$project->info($bind['pid'],'uid');
		if(!$bindower || $bindower['uid']!=$admin_info['id']){
			location_msg(L(1017));
		}
		}

		//删除设备 @author leiqianyong 2016-9-30 10:53 AM
		if($project->bind_del($id, $did)){
			location_msg(L(1445));
		}
		location_msg(L(1446));

		exit();
		$rst = $project->bind_edit($id,$did);
		if($rst < 1){
			location_msg(L(1446));
		}
		location_msg(L(1445));

	}

	/******************** function ********************/
	/**
	* 获取项目列表
	* @author xiezhihui 2014-6-9 16:42:31
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	*/
	function project_fun_get_project_list($data,$page=1,$pagesize=10){
		global $project;
		return $project->get_project_list($data,$page,$pagesize);
	}
	/**
	* 获取项目详细 根据项目ID
	* @author xiezhihui 2014-6-9 16:53:28
	* @param mixed $id
	* @return mysql对象
	*/
	function project_fun_get_project_info($id){
		global $project;
		if(!$id){
			return false;
		}
		return $project->info($id);
	}
	/**
	* 根据ID获取项目作业点
	*
	* @param mixed $id
	*/
	function project_fun_get_project_address_info($id){
		global $project;
		if(!$id){
			return false;
		}
		return $project->address_info($id);
	}

	/**
	* 获取项目地址列表
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	* @return mysql对象
	*/
	function project_fun_get_project_address_list($data,$page=1,$pagesize=10){
		global $project;
		return $project->get_project_address_list($data,$page,$pagesize);
	}

	/**
	* 获取项目绑定设备列表
	*
	* @param array $data
	* @param int $page
	* @param int $pagesize
	*/
	function project_fun_get_project_device_list($data,$page=1,$pagesize=10){
		global $project;
		return $project->get_bind_list($data,$page,$pagesize);
	}


	/**
	 * 查看绑定详情
	 * @param int $id
	 */
	function project_fun_get_bind_info($id){
		global $project;
		if(!$id)return false;
		return $project->bind_info($id);
	}

?>
